/**
 * 
 */
package tani.bnn.com.mbilling.utility;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author netesh.thukral
 * 
 */
public class ConnectionManager { //to implement thread pooling
	private Connection con;
	private static String dbUrl = "jdbc:mysql://localhost:3306/";
	private static String dbClass = "com.mysql.jdbc.Driver";
	private  ConnectionManager(){
		
	}
	
	public static synchronized Connection getNewConnection() throws Exception {
		ConnectionManager newCon = new ConnectionManager();
		String strUser = null;
		String strPwd = null;
		String strDB = null;
		Utility util = Utility.getAppUtility();
		try {
			strUser = util.getProperty(MBillingConstant.USER);
			strPwd = util.getProperty(MBillingConstant.PASSWORD);
			strDB = util.getProperty(MBillingConstant.DBNAME);
			if(newCon.con == null){
				Class.forName(dbClass);
				newCon.con = DriverManager.getConnection (dbUrl + strDB, strUser, DecodeEncodeUtil.decode(strPwd));
				System.out.println("connection succcessful");
			}
		}catch(ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
			throw new Exception("ClassNotFoundException : " + dbClass + " "+cnfe.getMessage());
		} catch(SQLException sqle) {
			sqle.printStackTrace();
			throw new Exception("SQLException : "  + sqle.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getCause() + " : " + dbClass + e.getMessage());
		}
		return newCon.con;
	}
	public static void main(String[] arg){
		try {
			Connection con = getNewConnection();
			con.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
